﻿namespace BEGDC.CC.Web.UserControls.EV
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using BEGDC.CC.BL.CCFlow.EV;
	using BEGDC.CC.Entity;
	using BEGDC.CC.BL.Common;
	using BEGDC.Utility.Castle;

	/// <summary>
	///		Summary description for APCommentCtrl.
	/// </summary>
	public class APCommentCtrl : BaseEV
	{
		protected System.Web.UI.WebControls.Label lblNo;
		protected System.Web.UI.WebControls.Label lblEvent;
		protected System.Web.UI.WebControls.Label lblAnswered;
		protected System.Web.UI.WebControls.TextBox txtAnswered;
		protected System.Web.UI.WebControls.Label lblIncluding;
		protected System.Web.UI.WebControls.ImageButton ibtnSubmitComment;
		protected System.Web.UI.WebControls.DataGrid dtGridAPComment;
		protected System.Web.UI.WebControls.TextBox txtIncluding;
		protected System.Web.UI.WebControls.Label lblAPCommentNo;
		protected System.Web.UI.WebControls.TextBox txtReply;

		protected int APID
		{
			get
			{
				if( Request.QueryString["APID"] == null )
				{
					if( ViewState["APID"] != null )
					{
						return Convert.ToInt32(ViewState["APID"]);
					}
					else
					{
						return 0;
					}
				}
				else
				{
					return Convert.ToInt32(Request.QueryString["APID"]);
				}
			}
			set
			{
				ViewState["APID"] = value;
			}
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if(!IsPostBack)
			{
				InitCommentInfo();
				InitCommentHistory();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.ibtnSubmitComment.Click += new System.Web.UI.ImageClickEventHandler(this.ibtnSubmitComment_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void InitCommentInfo()
		{
			IEV evService = AppContainer.Instance[ServiceConst.EVService] as IEV;
			this.lblAPCommentNo.Text = Convert.ToString(evService.GetMaxAPCommentID( this.APID ) + 1 );
			this.txtAnswered.Text = HttpContext.Current.User.Identity.Name;

			this.txtReply.Text = "";
			this.txtIncluding.Text = "";
		}
		private void InitCommentHistory()
		{
			IEV evService = AppContainer.Instance[ServiceConst.EVService] as IEV;
			DataTable commentsTable = evService.GetAPCommentsHistory( this.APID );

			dtGridAPComment.DataSource = commentsTable.DefaultView;
			dtGridAPComment.DataBind();
			
		}

		private Entity.APComment BuildComment2Save()
		{
			APComment comment = new APComment();
			comment.APNo = Convert.ToInt32( this.lblAPCommentNo.Text);
			comment.Events = this.txtReply.Text;
			comment.AnsweredBy = this.txtAnswered.Text;
			comment.Including = this.txtIncluding.Text;
			comment.Date = new Nullables.NullableDateTime(System.DateTime.Now);

			IEV evService = AppContainer.Instance[ServiceConst.EVService] as IEV;
			comment.ActionPoint = evService.GetAPByID( this.APID );

			return comment;
		}
		
		private void ibtnSubmitComment_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				IEV evService = AppContainer.Instance[ServiceConst.EVService] as IEV;
				evService.SubmitAPComment( this.BuildComment2Save() );
				
				this.InitCommentHistory();
				InitCommentInfo();
			}
			catch
			{
			}
		}
	}
}
